<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/11/17
 * Time: 21:38
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller;

use app\common\business\Package as BusObj;
use app\common\lib\Show;
use think\App;
use think\Exception;

class Package extends Base
{
    protected BusObj $busObj;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->busObj = new BusObj();
    }

    public function add()
    {
        $transferData = [];
        try {
            $transferData = (new \app\common\business\Service())->getServiceByList();
        } catch (\Exception $e) {
            throw new Exception("内部错误");
        }
        $this->assign('transferData',Show::json_encode($transferData));
        return parent::add(); // TODO: Change the autogenerated stub
    }
    public function edit()
    {
        $id = $this->request->param('eid',0,'intval');
        //查询订单
        try {
            $package = $this->busObj->getBaseById($id,'service');
        } catch (\Exception $e) {
            throw new Exception("内部错误");
        }

        try {
            $transferData = (new \app\common\business\Service())->getServiceByList();
        } catch (\Exception $e) {
            throw new Exception("内部错误");
        }
        $values = [];
        if(!empty($package['service']) && !empty($transferData)){
            $lists = Show::json_decode($package['service']);
            $list_transferData = array_column($transferData,'id');
            $res = array_intersect($list_transferData,$lists);
            if(!empty($res)){
                foreach ($transferData as $k=>$v){
                    foreach ($res as $item){
                        if($v['id'] == $item){
                            $values[] = $item;
                        }
                    }
                }
            }

        }
        $this->assign('values',json_encode($values));
        $this->assign('transferData',Show::json_encode($transferData));
        return parent::edit(); // TODO: Change the autogenerated stub
    }
}